//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    #region NameSpace
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Workflow.ComponentModel;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using System.Workflow.Activities;
    using System.ComponentModel;
    #endregion

    /// <summary>
    /// 
    /// </summary>
    public partial class WorkflowStackHeader : UserControl, INotifyPropertyChanged
    {
        #region Declaration
        /// <summary>
        /// Holds the flag for displaying the Left connectors
        /// </summary>
        private bool hideLeftConnector = false;

        /// <summary>
        /// Holds the flag for displaying the Right connectors
        /// </summary>
        private bool hideRighConnector = false;

        /// <summary>
        /// If valid mode or not.
        /// </summary>
        private bool updateAllowedMode;

        #endregion

        #region Events

        public event EventHandler<ModelEventArgs> UpdateClick;

        #endregion

        /// <summary>
        /// Constructor 
        /// Collapse the connector rectangles if Activity is parent
        /// </summary>
        /// <param name="isParent"></param>
        public WorkflowStackHeader(bool isParent, BaseModel model)
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(this.OnLoaded);
            this.hideLeftConnector = isParent;
            this.hideRighConnector = isParent;
            this.DataContext = model;
            this.DisplayConnector(); 
        }

        #region Properties

        /// <summary>
        /// Get and Sets the Header BackGround color.
        /// </summary>
        public Brush HeaderBackground
        {
            get
            {
                return this.headerBorder.Background;
            }
            set
            {
                this.headerBorder.Background = value;
            }
        }

        /// <summary>
        /// Get and Sets the Header Text.
        /// </summary>
        public string HeaderText
        {
            get
            {
                return this.headerTextBlock.Text;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    this.headerTextBlock.Text = string.Empty;
                }
                else
                {
                    this.headerTextBlock.Text = value;
                }
            }
        }

        /// <summary>
        /// Get and Sets the Header Image.
        /// </summary>
        public ImageSource HeaderImage
        {
            get
            {
                return this.headerImage.Source;
            }
            set
            {
                this.headerImage.Source = value;
            }
        }

        /// <summary>
        /// Gets the left control flow connector.
        /// </summary>
        public FrameworkElement LeftControlFlowConnectionElement
        {
            get { return this.rectangleLeft; }
        }

        /// <summary>
        /// Gets the right control flow connector.
        /// </summary>
        public FrameworkElement RightControlFlowConnectionElement
        {
            get { return this.rectangleRight; }
        }

        /// <summary>
        /// Gets a value indicating whether valid mode.
        /// </summary>
        /// <value><c>true</c> if valid mode; otherwise, <c>false</c>.</value>
        public bool UpdateAllowedMode
        {
            get
            {
                return this.updateAllowedMode;
            }

            set
            {
                this.updateAllowedMode = value;
                this.RaisePropertyChanged("UpdateAllowedMode");
            }
        }

        #endregion

        /// <summary>
        /// Invokes the Left and right connector display methods
        /// </summary>
        private void DisplayConnector()
        {
            this.DisplayLeftConnector();
            this.DisplayRightConnector(); 
        }

        /// <summary>
        /// Display Left connector
        /// </summary>
        private void DisplayLeftConnector()
        {
            if (this.hideLeftConnector == true)
            {
                this.rectangleLeft.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.rectangleLeft.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// Display the right connector
        /// </summary>
        private void DisplayRightConnector()
        {
            if (this.hideRighConnector == true)
            {
                this.rectangleRight.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.rectangleRight.Visibility = Visibility.Visible;
                
            }
        }

        /// <summary>
        /// Called on update click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnUpdateClick(object sender, RoutedEventArgs e)
        {
            if (this.UpdateClick != null)
            {
                this.UpdateClick.Invoke(this, new ModelEventArgs(this.DataContext as BaseModel));
            }
        }

        /// <summary>
        /// Called when loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            string composerCtrlName = (string)this.GetValue(ComposerControl.ComposerControlNameProperty);
            ComposerControl composerCtrl = Application.Current.MainWindow.FindName(composerCtrlName) as ComposerControl;
            if (composerCtrl != null && composerCtrl.CurrentRenderMode == RenderMode.ComposerMode)
            {
                this.UpdateAllowedMode = true;
            }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the property changed.
        /// </summary>
        /// <param name="name">The name.</param>
        protected void RaisePropertyChanged(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion
    }
}